package com.ruoyi.miniapp.service.impl;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.miniapp.domain.MiniappPost;
import com.ruoyi.miniapp.mapper.MiniappPostMapper;
import com.ruoyi.miniapp.mapper.MiniappUserPostMapper;
import com.ruoyi.miniapp.service.IMiniappPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 岗位信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-16
 */
@Service
public class MiniappPostServiceImpl implements IMiniappPostService
{
    @Autowired
    private MiniappPostMapper miniappPostMapper;
    @Autowired
    private MiniappUserPostMapper miniappUserPostMapper;
    /**
     * 查询岗位信息
     *
     * @param postId 岗位信息主键
     * @return 岗位信息
     */
    @Override
    public MiniappPost selectMiniappPostByPostId(Long postId)
    {
        return miniappPostMapper.selectMiniappPostByPostId(postId);
    }

    /**
     * 查询岗位信息列表
     *
     * @param miniappPost 岗位信息
     * @return 岗位信息
     */
    @Override
    public List<MiniappPost> selectMiniappPostList(MiniappPost miniappPost)
    {
        return miniappPostMapper.selectMiniappPostList(miniappPost);
    }

    /**
     * 新增岗位信息
     *
     * @param miniappPost 岗位信息
     * @return 结果
     */
    @Override
    public int insertMiniappPost(MiniappPost miniappPost)
    {
        miniappPost.setCreateTime(DateUtils.getNowDate());
        return miniappPostMapper.insertMiniappPost(miniappPost);
    }

    /**
     * 修改岗位信息
     *
     * @param miniappPost 岗位信息
     * @return 结果
     */
    @Override
    public int updateMiniappPost(MiniappPost miniappPost)
    {
        miniappPost.setUpdateTime(DateUtils.getNowDate());
        return miniappPostMapper.updateMiniappPost(miniappPost);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位信息主键
     * @return 结果
     */
    @Override
    public int deleteMiniappPostByPostIds(Long[] postIds)
    {
        for (Long postId : postIds)
        {
            MiniappPost post = selectMiniappPostByPostId(postId);
            if (countUserPostById(postId) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return miniappPostMapper.deleteMiniappPostByPostIds(postIds);
    }

    /**
     * 删除岗位信息信息
     *
     * @param postId 岗位信息主键
     * @return 结果
     */
    @Override
    public int deleteMiniappPostByPostId(Long postId)
    {
        return miniappPostMapper.deleteMiniappPostByPostId(postId);
    }

    @Override
    public boolean checkPostNameUnique(MiniappPost post) {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        MiniappPost info = miniappPostMapper.checkPostNameUnique(post.getPostName());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeUnique(MiniappPost post)
    {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        MiniappPost info = miniappPostMapper.checkPostCodeUnique(post.getPostCode());
        System.out.println(StringUtils.isNotNull(info));
        System.out.println(info);
        System.out.println(postId.longValue());
        System.out.println(info.getPostId().longValue());
        System.out.println(info.getPostId().longValue() != postId.longValue());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<MiniappPost> selectPostAll()
    {
        return miniappPostMapper.selectPostAll();
    }


    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectPostListByUserId(Long userId)
    {
        return miniappPostMapper.selectPostListByUserId(userId);
    }


    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    public int countUserPostById(Long postId)
    {
        return miniappUserPostMapper.countUserPostById(postId);
    }
}
